﻿namespace EyeOpen.TimeAssistant.Data.IntegrationTest
{
    using System;
    using System.Linq;
    using EyeOpen.Domain;
    using EyeOpen.Security.Server.BootStrapper;
    using EyeOpen.Test.Patch;
    using EyeOpen.TimeAssistant.Data;
    using EyeOpen.TimeAssistant.Domain;
    using EyeOpen.TimeAssistant.Queries;
    using EyeOpen.TimeAssistant.Server.BootStrapper;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using SharpTestsEx;

    /// <summary>
    /// Tests the <see cref="TimeAssistantDataContext"/>.
    /// </summary>
    [TestClass]
    public class TimeAssistantDataContextTest : TestBase
    {
        [TestInitialize]
        public void Initialize()
        {
            new TimeAssistantDatabaseFactory().CreateDatabase();
        }

        [TestMethod]
        [Owner("Matteo Migliore")]
        [Description("Integration.TimeAssistant.Data")]
        public void CreateTimeAssistantDataBaseExpectedWorks()
        {
        }

        [TestMethod]
        [Owner("Matteo Migliore")]
        [Description("Integration.TimeAssistant.Data")]
        public void AddAProjectAndReturnItByQueryExpectedGetTheRightProjectProjection()
        {
            var projectNameByPriority =
                new ProjectNameByPriorityQuery
                {
                    Priority = 1
                };

            var user =
                new User
                {
                    Id = Guid.NewGuid(),
                };

            var meta =
                new Meta
                    {
                        Id = Guid.NewGuid(),
                        Title = "Title1",
                        Description = "Description1",
                        User = user
                    };

            var project1 =
                new UserStory
                {
                    Id = Guid.NewGuid(),
                    Name = "Project1",
                    Priority = 1,
                    User = user,
                    Meta = meta
                };

            var project2 =
                new UserStory
                {
                    Id = Guid.NewGuid(),
                    Name = "Project2",
                    Priority = 2,
                    User = user,
                    Meta = meta
                };

            int projectNameCount;

            using (var timeAssistantDataContext = new TimeAssistantDataContextFactory().CreateDataContext())
            {
                timeAssistantDataContext.Add(user);
                timeAssistantDataContext.Add(project1.Meta);
                timeAssistantDataContext.Add(project1);
                timeAssistantDataContext.Add(project2);
                timeAssistantDataContext.SaveChanges();

                projectNameCount = 
                    timeAssistantDataContext
                    .GetByQuery(projectNameByPriority)
                    .Count();
            }

            projectNameCount
                .Should("Projects by priority wrong count.")
                .Be
                .EqualTo(1);
        }
    }
}